﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Elementum
{
    public class GameDoor : Actor,ChangeStateable
    {
        private bool _isOpen;

        Animation _openDoor;
        Animation _closeDoor;

        public delegate void OpenDoorActionHolder();
        public event OpenDoorActionHolder OpenDoorAction;

        public GameDoor(String Id, World world, Stage stage)
            : base(world, stage)
        {
            _openDoor = new Animation("images/items/door_open", new Point(60, 100));
            CloseDoorAnimation = new Animation("images/items/door", new Point(60, 100));
            SaveDoorID = Id;
            _body.SetUserData("door");
            if (!StageManager.Save.StageStateTable[StageManager.CurrentStateID].DoorStateTable.ContainsKey(SaveDoorID))
            {
                DoorState doorState = new DoorState();
                try
                {
                    StageManager.Save.StageStateTable[StageManager.CurrentStateID].DoorStateTable.Add(SaveDoorID, doorState);
                }
                catch (ArgumentException)
                {
                    throw new Exception("An GameDoor with Key = " + SaveDoorID + " already exists.");
                }

            }

            IsOpened = StageManager.Save.StageStateTable[StageManager.CurrentStateID].DoorStateTable[SaveDoorID]._isOpen;
        }

        public string SaveDoorID
        {
            get;
            set;
        }

        public bool IsOpened
        {
            get { return _isOpen; }
            set {
                _isOpen = value;
                if (value)
                {
                    StageManager.Save.StageStateTable[StageManager.CurrentStateID].DoorStateTable[SaveDoorID]._isOpen = value;
                    CurrentAnimation = OpenDoorAnimation;
                    _filter.maskBits = 0x00;
                    if (OpenDoorAction != null)
                        OpenDoorAction();
                }
                else {
                    //if(CloseDoorAnimation!=null)
                    StageManager.Save.StageStateTable[StageManager.CurrentStateID].DoorStateTable[SaveDoorID]._isOpen = value;
                    CurrentAnimation = CloseDoorAnimation;
                    _filter.maskBits = GroundMask | PlayerCategory;
                }
                if(_fixture!=null)
                    _fixture.SetFilterData(ref _filter);
            }
        }

        public Animation CloseDoorAnimation
        {
            set { _closeDoor = value;
                if(!_isOpen)
            CurrentAnimation = value;
            }
            private get { return _closeDoor; }
        }

        public Animation OpenDoorAnimation
        {
            set
            {
                _openDoor = value;
                CurrentAnimation = value;
            }
            private get { return _openDoor; }
        }

        public GameDoor(World world, Stage stage)
            : base(world,stage)
        {
            _openDoor = new Animation("images/items/door_open", new Point(60, 100));
            CloseDoorAnimation = new Animation("images/items/door", new Point(60, 100));
        }

        

        public override void CreatBody(World world)
        {
            this._world = world;
            BodyDef bodyDef = new BodyDef();
            _body = world.CreateBody(bodyDef);
            _body.Owner = this;
            DoorID = "Door";
            _body._type = BodyType.Static;
            _body.SetUserData("door");
        }

        public string DoorID
        {
            get;
            set;
        }

        protected override void ConfigFilter()
        {
            base.ConfigFilter();
            _filter.categoryBits = GroundCategory;
            _filter.maskBits = GroundMask;
        }

        public override void Update(int delta)
        {
            base.Update(delta);
            CurrentAnimation.LayerDepth = 0.8f;
        }



        public void ChangeState()
        {     
            IsOpened = !IsOpened;
            //StageManager.Save.StateStateTable[StageManager.CurrentStateID].DoorStateTable[SaveDoorID]._isOpen = IsOpened;
            MusicManager.PlayMusic("doorlock");
        }

        public void ChangeState(bool active)
        {
            ChangeState();
        }
    }
}
